package me.keiwu.user.controller.rpc;

import com.alibaba.fastjson.JSON;
import me.keiwu.user.domain.CombAvatar;
import me.keiwu.user.domain.Item;
import me.keiwu.user.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by kei on 21/11/2017.
 */
@RestController
@RequestMapping("/rpc/item")
public class ItemRpcController {

    @Autowired
    private ItemService itemService;


    @RequestMapping("/suit/list")
    public Page<Item> listSuit(int page, int size) {
        return itemService.getAllSuits(page, size);
    }

    @RequestMapping("/suit/add")
    public Item addSuit(String name,
                           String imgUrl,
                           String itemDetail,
                           Integer costGold,
                           boolean shareGet,
                           boolean shelve) {
        return itemService.addSuit(name, imgUrl, itemDetail, costGold, shareGet, shelve);
    }

    @RequestMapping("/suit/update")
    public Item updateSuit(Integer itemId,
                              String name,
                              String imgUrl,
                              String itemDetail,
                              Integer costGold,
                              boolean shareGet,
                              boolean shelve) {
        return itemService.updateSuit(itemId, name, imgUrl, itemDetail, costGold, shareGet, shelve);
    }




    // 单件
    @RequestMapping("/single/list")
    public Page<Item> listSingle(int page, int size) {
        return itemService.getAllSingles(page, size);
    }

    @RequestMapping("/single/add")
    public Item addSingle(String name,
                             String imgUrl,
                             Integer costGold,
                             boolean shareGet,
                             boolean shelve,
                             String combItemsJson) {
        List<CombAvatar> combAvatarList = JSON.parseArray(combItemsJson, CombAvatar.class);
        if (combAvatarList == null) return null;

        return itemService.addSingle(name, imgUrl, costGold, shareGet, shelve, combAvatarList);
    }

    @RequestMapping("/single/update")
    public Item updateSingle(Integer itemId,
                                String name,
                                String imgUrl,
                                Integer costGold,
                                boolean shareGet,
                                boolean shelve,
                                String combItemsJson) {
        List<CombAvatar> combAvatarList = JSON.parseArray(combItemsJson, CombAvatar.class);
        if (combAvatarList == null) return null;

        return itemService.updateSingle(itemId, name, imgUrl, costGold, shareGet, shelve, combAvatarList);
    }




    @RequestMapping("/shelve")
    public boolean shelve(@RequestParam("itemId") Integer itemId, @RequestParam("shelve") boolean shelve) {
        Item item = itemService.shelveItem(itemId, shelve);
        return item != null;
    }

}
